package com.cupdata.zicon.provider;

import org.apache.thrift.TException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cupdata.thrift.zicon.interfaces.HelloService;
import com.cupdata.thrift.zicon.interfaces.UserDto;
import com.cupdata.thrift.zicon.interfaces.UserService;
import com.cupdata.zicon.annotation.ThriftClient;
import com.cupdata.zicon.thrift.client.pool.ThriftClientPooledObjectFactory;

@Controller
public class ServiceProvider {
	
	@ThriftClient(serviceId="cogate-thrift-server", serviceName="helloService")
	private HelloService.Client helloService;
	
	@ThriftClient(serviceId="cogate-thrift-server", serviceName="userService")
	private UserService.Client userService;
	
	private Logger log = LoggerFactory.getLogger(getClass());
	
	
	@ResponseBody
	@RequestMapping("/test")
	public String  test(){
		String sayHello = null;
		String sayName = null;
		try {
			/**
			 * 现在列表获取没问题，但是sayName方法执行错误，返回Null
			 * org.apache.thrift.transport.TTransportException:
			 * Cannot write to null outputStream
			 */
			 sayHello = helloService.sayHello();
//			 sayName = userService.sayName("123");
			System.out.println(sayHello + sayName); 
		} catch (TException e) {
			ThriftClientPooledObjectFactory.setToCreate(true);
			log.info("请求失败 :" + e);
		}
		return sayHello + " : " + sayName;
		
	}
	
	@ResponseBody
	@RequestMapping("/test2")
	public String test2(){
		String sayName = null;
		UserDto userDto = null;
		try {
			 sayName = helloService.sayName("test2");
			 userDto = userService.getUser();
		} catch (TException e) {
			e.printStackTrace();
		}
		return sayName + userDto;
	}
}
